package com.hx.mylombok.task.impl;

import com.hx.mylombok.task.AbstractFileTask;
import com.hx.mylombok.task.FileTask;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @remark --
 * @author: GuoXiaolin
 * @date 2019/4/24 10:41
 * @Version 1.0
 * @Derction
 */
public class RepositoryTask extends AbstractFileTask implements FileTask {

    public RepositoryTask(String className) {
        super(className);
    }

    @Override
    public void generateFile(Map<String, String> setterMap, ProcessingEnvironment processingEnv) throws IOException {

        JavaFileObject builderControllerFile = processingEnv.getFiler().
                createSourceFile(packageName + ".repository.IRepository"+sufixName);
        try (PrintWriter out = new PrintWriter(builderControllerFile.openWriter())) {

            if (packageName != null) {
                out.print("package ");
                out.print(packageName);
                out.println(".repository;");
                out.println();
            }
            out.println("import "+packageName+".domain."+sufixName+";");
            out.println("import org.springframework.data.jpa.repository.JpaRepository;");
            out.println();
            out.println("public interface IRepository"+sufixName+" extends JpaRepository<"+sufixName+",Integer"+"> {");
            out.println();
            out.println("}");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            logger.info("RepositoryTask of " + simpleClassName + " is over");
        }
    }
}
